#include  	"main.h"
#include 	"system.h"


/***********************
���õ��� loT ƽ̨��ַ�Ͷ˿�
*************************/
uint8 BC26_QLWSERV(uint8 *TelecomPlatformIP,uint8*TelecomPlatformPort)
{ 
	uint8 i=0,sockedid=0,result=0 ;
	uint8 ATStrBuf[50]={0};
	const UINT8 Buf1[2]={'"',0 };
	const UINT8 Buf2[3]={'"',',',0};
	printf("\n--->>>Config IP and Port...\n");
	
	sprintf(ATStrBuf,"AT+QLWSERV=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,TelecomPlatformIP);
	strcat(ATStrBuf,Buf2);
	strcat(ATStrBuf,TelecomPlatformPort);
	strcat(ATStrBuf,"\r\n\0");
	printf("ATStrBuf=%s\n",ATStrBuf);

	
	i=3;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(1000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,"AT+QLWSERV=",strlen("AT+QLWSERV="))==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("BC26_QLWSERV success\n");
			return TRUE;
		}
		else
		{
			NB_Delay(2000);	
		}		
	}
		
	printf("BC26_QLWSERV fail\n");
	return FALSE;
	
}

/***********************
���õ��� loT ƽ̨����.���豸�� IMEI �ŵ�
*************************/
uint8 BC26_QLWCONF(uint8 *IMEI)
{ 
	uint8 i=0,sockedid=0,result=0 ;
	uint8 ATStrBuf[50]={0};
	const UINT8 Buf1[2]={'"',0 };
	printf("\n--->>>Config Equipment IMEI\n");
	
	sprintf(ATStrBuf,"AT+QLWCONF=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,IMEI);
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,"\r\n\0");
	printf("ATStrBuf=%s\n",ATStrBuf);
	
	i=3;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(1000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,"AT+QLWCONF=",strlen("AT+QLWCONF="))==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("BC26_QLWCONF success\n");
			return TRUE;
		}
		else
		{
			NB_Delay(2000);	
		}		
	}
		
	printf("BC26_QLWCONF fail\n");
	return FALSE;
	
}

/***********************
	���� LwM2M ����
*************************/
uint8 BC26_QLWADDOBJ(void)
{ 
	uint8 i=0,sockedid=0,result=0;
	
	uint8  ATStrBuf_UPchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','0',',','1',',','"','0','"','\r','\n',0};
	uint8  ATStrBuf_Downchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','1',',','1',',','"','0','"','\r','\n',0};	
	printf("\n--->>>add LwM2M obj\n");
	
	i=3;
	while(i)
	{
	    --i;
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf_UPchannle,strlen(ATStrBuf_UPchannle));
		NB_Delay(1000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,ATStrBuf_UPchannle,strlen(ATStrBuf_UPchannle)-2)==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("BC26_QLWCONF up success\n");
			
			break;
		}	
		else
		{
			NB_Delay(2000);	
		}		
	}
		
	if(i==0)
	{
		printf("BC26_QLWCONF up fail\n");	
	}
	

	i=3;
	while(i)
	{
		--i;
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf_Downchannle,strlen(ATStrBuf_Downchannle));
		NB_Delay(1000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,ATStrBuf_Downchannle,strlen(ATStrBuf_Downchannle)-2)==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("BC26_QLWCONF down success\n");
			return TRUE;
		}
		else
		{
			NB_Delay(2000);	
		}		
	}
	printf("BC26_QLWCONF down fail\n");	
	return  FALSE; 
			
}


/***********************
  ����� loT ƽ̨����ע������
*************************/
uint8 BC26_QLWOPEN(void)
{ 
	uint8 i=0,sockedid=0,result=0,j=0;
	uint8  ATStrBuf[14+1]={'A','T','+','Q','L','W','O','P','E','N','=','1','\r','\n',0};
	printf("\n--->>>Send Registration request\n");
	i=2;
	while(i)
	{
		--i;
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(500);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		
	  
		if((strncmp(gNB_ReceData->buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("BC26 Regist  success\n");
			/*************
			len=26
			AT+QLWOPEN=1
			OK
			
			CONNECT OK  
			��ʱ��ܿ��յ� CONNECT OK
			*************/
			if(strstr(gNB_ReceData->buf,"CONNECT OK")==NULL)
			{
				j=0;
				gNB_ReceData->len=0 ;
				memset(gNB_ReceData->buf,0,Uart_ReceBufLen);			
				while(1)
				{
					NB_Delay(1000);
					printf("*");
					if(++j>128)
					{
						break;
					}
					if(gNB_ReceData->len>0)
					{
						
						printf("len=%d\n",gNB_ReceData->len);
						gNB_ReceData->buf[gNB_ReceData->len]=0 ;
						printf("%s\n",gNB_ReceData->buf);
						if(strstr(gNB_ReceData->buf,"CONNECT OK")!=NULL)
						{
								break;
						}
						else if(strstr(gNB_ReceData->buf,"CONNECT FAIL")!=NULL)
						{
								return  FALSE; 	
						}
						else
						{
							gNB_ReceData->len=0 ;
							memset(gNB_ReceData->buf,0,Uart_ReceBufLen);	

						}
						
					}
				}
			}


			
			j=0;
			gNB_ReceData->len=0 ;
			memset(gNB_ReceData->buf,0,Uart_ReceBufLen);	
			while(1)
			{
				NB_Delay(1000);
				printf("*");
				if(++j>60)
				{
					break;
				}
				if(gNB_ReceData->len>0)
				{
					NB_Delay(1000);
					printf("--len=%d\n",gNB_ReceData->len);
					gNB_ReceData->buf[gNB_ReceData->len]=0 ;
					printf("%s\n",gNB_ReceData->buf);
					if(strstr(gNB_ReceData->buf,"+QLWOBSERVE:")!=NULL)
					{
						return TRUE;
					}
					else
					{
						gNB_ReceData->len=0 ;
						memset(gNB_ReceData->buf,0,Uart_ReceBufLen);		

					}

				}
			}			
			
		}
		else
		{
			NB_Delay(2000);	
		}		
	}
	printf("BC26 Regist fail\n");
	return  FALSE; 
			
}



/***********************
 ����� IoT ƽ̨���͸�������
*************************/
uint8 BC26_QLWUPDATE(void)
{ 
	uint8 i=0,sockedid=0,result=0,j=0;
	uint8  ATStrBuf[14+1]={'A','T','+','Q','L','W','U','P','D','A','T','E','\r','\n',0};

	printf("\n--->>>Send update request\n");
	
	i=2;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(500);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		
	  
		if((strncmp(gNB_ReceData->buf,"AT+QLWUPDATE",strlen("AT+QLWUPDATE"))==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("QLWUPDATE success\n");


			if(strstr(gNB_ReceData->buf,"UPDATE OK")==NULL)
			{
				j=0;
				gNB_ReceData->len=0 ;
				memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
				
				while(1)
				{
					NB_Delay(1000);
					printf("*");
					if(++j>60)
					{
						break;
					}
					if(gNB_ReceData->len>0)
					{
						NB_Delay(1000);
						printf("len=%d\n",gNB_ReceData->len);
						gNB_ReceData->buf[gNB_ReceData->len]=0 ;
						printf("%s\n",gNB_ReceData->buf);
						if(strstr(gNB_ReceData->buf,"UPDATE OK")!=NULL)
						{
							printf("update success\n");
							return TRUE;
						}
						else	if(strstr(gNB_ReceData->buf,"UPDATE FAIL")!=NULL)
						{	
							printf("update fail\n");
							return FALSE;
						}	
						

					}
				}
			}
			else
			{
				printf("Update Success\n");
				return TRUE;

			}
			
			
		}
		else
		{
			NB_Delay(2000);	
		}
		
	}
		
	printf("BC26_QLWUPDATE fail\n");
	return FALSE;
	
}



/***********************
 ���ÿ�ѡ����
 ��������dataformat
 0 Ϊ�ı��ַ�����1λ16�����ַ���
*************************/
uint8 BC26_QLWCFG(void)
{ 
	uint8 i=0,sockedid=0,result=0 ;
	
	uint8  ATStrBuf[28+1]={'A','T','+','Q','L','W','C','F','G','=','"','d','a',\
	't','a','f','o','r','m','a','t','"',',','0',',','0','\r','\n',0};

	printf("\n--->>>Config dataformat\n");
	
	i=3;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(1000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("AT+QLWCFGsuccess\n");
			
			return TRUE;
		}	
		else
		{
			NB_Delay(2000);	
		}
	}
		
	printf("AT+QLWCFG fail\n");
	return FALSE;
	
}


/************************************************************
	�������ݵ�ƽ̨
	����iotƽ̨������ͨ����19/1/0,����ͨ��19/0/0
**************************************************************/
uint16 SendDataToTelecomPlatform(uint8*buffer,uint16 len)
{
    uint8 i=3,j=0;
    uint8 ATStrBuf[200]={0};
    sprintf(ATStrBuf,"AT+QLWDATASEND=19,0,0,%d,%s,0x0100\r\n",len,buffer);

	

	
    while(i--)
    {
        gNB_ReceData->len=0;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf)+1);
        NB_Delay(1000);
        printf("len=%d\n",gNB_ReceData->len);
        gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		
        
        if((strncmp(gNB_ReceData->buf,"AT+QLWDATASEND=",strlen("AT+QLWDATASEND="))==0)&&\
        (strstr(gNB_ReceData->buf,"OK")!=NULL))
        {
        	printf("send  success\n");

			if(strstr(gNB_ReceData->buf,"SEND OK")==NULL)
			{
				gNB_ReceData->len=0 ;
				memset(gNB_ReceData->buf,0,Uart_ReceBufLen);        	
	            
				j=30;
				while(j--)
				{
					printf("*");
					NB_Delay(1000);
					if(gNB_ReceData->len>0)
					{
						printf("len=%d\n",gNB_ReceData->len);
						gNB_ReceData->buf[gNB_ReceData->len]=0 ;
						printf("%s\n",gNB_ReceData->buf);
					  
						if(strstr(gNB_ReceData->buf,"SEND OK")!=NULL)
						{
							 printf("send  data success\n");
							 return	TRUE;
						}	
						else if(strstr(gNB_ReceData->buf,"SEND FAIL")!=NULL)
						{
							printf("send  data fail\n");
							return FALSE;
						}
					}
				}
			}
			else
			{
				printf("Send  Data Success\n");
				return	TRUE;
			}

        }
        else 
        {
            printf("Send  ERR\n");
			NB_Delay(2000);
        }
        
    }

    return FALSE ;
}


#if 0
/***********************
 ��ѯ CON ��Ϣ�ķ���״̬
 AT+QLWDATASTATUS?
+QLWDATASTATUS: 4

OK
����ֵ����:
0 ��δ����
1 �ѷ��ͣ��ȴ� loT ƽ̨����Ӧ
2 ����ʧ��
3 ���ͳ�ʱ
4 ���ͳɹ�
5 ��ƽ̨�ܾ�

���� CON ��Ϣ����Ӧ SEND OK �� SEND FAIL �����ʱʱ��Ĭ��ԼΪ 128 ��

*************************/
uint16 BC26_QLWDATASTATUS(void)
{ 
	uint8 i=0,sockedid=0,result=0 ; 
	uint8  ATStrBuf[19+1]={'A','T','+','Q','L','W','D','A','T','A','S','T',\
	'A','T','U','S','?','\r','\n',0};
	printf("\n--->>>Enquiry send status\n");
	
	i=128;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(1000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			 
            result=UtilGetIntFlag(gNB_ReceData->buf,"+QLWDATASTATUS:","\r\n");
            printf("status=%d\n",result);
            if(result==4)
            {
            	 printf("send data success\n");
				return TRUE;
            }
			else if((result==5)||(result==3)||(result==2))
			{
				printf("send data fail\n");
				return FALSE;
			}
			else if(result==1)
			{
				printf("wait\n");		
			}
		}		
	}
		
	printf("AT+QLWCFG fail\n");
	return FALSE;
	
}
#endif
/***********************
 ����ƽ̨����,���ݳ�������Ϊ100
*************************/

uint16	ReceiveMessageFromTelecomPlatform(uint8*recebuffer)
{ 

	uint8 i=0,sockedid=0,len=0 ; 
	uint8  ATStrBuf[14+1]={'A','T','+','Q','L','W','R','D','=','1','0','0','\r','\n',0};
	uint8 *start;

	printf("\n--->>>Receive Data from Telecom\n");
	/******************
	AT+QLWRD=100
	+QLWRD: 12,0
	111111111111

	OK
	***************/
	i=10;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(1000);
		if((strncmp(gNB_ReceData->buf,"AT+QLWRD=100",strlen("AT+QLWRD=100"))==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			start = strstr(gNB_ReceData->buf, "+QLWRD:");
			if (start != NULL)
			{
				len=UtilGetIntFlag(start,"+QLWRD:",",");
				if(len>0)
				{
					printf("LEN=%d\n",len);
					start = strstr(gNB_ReceData->buf, ",");
					if (start != NULL)
					{
						start = strstr(start, "\r\n");
						if (start != NULL)
						{	
						
							memcpy(recebuffer,start+strlen("\r\n"),len);
							recebuffer[len]=0;
							printf("data=%s\n",recebuffer);
							return len;
						}
					}
					
				}
				
				
			} 				
		}		
	}
	
	printf("NO data to receive\n");
	return 0;

}
/***********************
	ɾ�� LwM2M ����
 	�����쳣�����Ҫ����������ز�������Ӧ������֮ǰִ�� AT+QLWDEL��
 	
	AT+QLWDEL
	OK

	AT+QLWDEL success 	
*************************/
uint8 BC26_QLWDEL(void)
{ 
	uint8 i=0,sockedid=0,result=0 ;
	
	uint8  ATStrBuf[11+1]={'A','T','+','Q','L','W','D','E','L','\r','\n'};
	printf("\n--->>>ɾ�� LwM2M ����\n");
	i=3;
	while(i--)
	{
		gNB_ReceData->len=0 ;
		memset(gNB_ReceData->buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		NB_Delay(2000);
		printf("len=%d\n",gNB_ReceData->len);
		gNB_ReceData->buf[gNB_ReceData->len]=0 ;
		printf("%s\n",gNB_ReceData->buf);
	  
		if((strncmp(gNB_ReceData->buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(gNB_ReceData->buf,"OK")!=NULL))
		{
			printf("AT+QLWDEL success\n");
			
			return TRUE;
		}
		else
		{
			NB_Delay(2000);
		}
	}
		
	printf("AT+QLWDEL fail\n");
	return FALSE;
	
}

